package com.common.help;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class Test3 {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		long start = System.currentTimeMillis();
		ExecutorService pool = Executors.newCachedThreadPool();
		List<Callable<Integer>> list = new ArrayList<Callable<Integer>>();
		for(int i=0;i<10;i++){
			list.add(new MyThread(i));
		}
		List<Future<Integer>> futures = pool.invokeAll(list);
		pool.shutdown();
		int sum=0;
		for(Future<Integer> future:futures){
			sum+=future.get();
		}
		System.out.println(sum);
		long end = System.currentTimeMillis();
		System.err.println((double)(end-start)/1000);
	}
}

class MyThread implements Callable<Integer>{
	
	private Integer num;
	
	public MyThread(Integer num){
		this.num= num;
	}

	@Override
	public Integer call() throws Exception {
		//Thread.sleep(1000);
		return num;
	}
	
}